import {createRouter, createWebHashHistory} from 'vue-router'
import index from '../views/index.vue'
import {getLoginUser} from '@/utils/common';


const routes = [
    {
        path: '/',
        name: 'index',
        component: import('@/views/join.vue'),
        meta: {
            'title': "全国执业兽医资格考试"
        }
    },
    {
        path: '/join',
        name: 'join',
        component: () => import('@/views/join.vue'),
        meta: {
            'title': "全国执业兽医资格考试"
        }
    },
    {
        path: '/enter',
        name: 'enter',
        component: () => import('@/views/enter.vue'),
        meta: {
            'title': "登录"
        }
    },
    {
        path: '/agreement',
        name: 'agreement',
        component: () => import('@/views/agreement.vue'),
        redirect: '/agreement/instructions',
        meta: {
            'title': "考生须知"
        },
        children: [
            {
                path: 'instructions',
                name: 'instructions',
                component: () => import('@/views/instructions.vue'),
            },
            {
                path: 'help',
                name: 'help',
                component: () => import('@/views/help.vue'),
            }
        ]
    },
    {
        path: '/exam',
        name: 'exam',
        component: () => import('@/views/exam.vue'),
        meta: {
            'title': "考试"
        }
    },
    {
        path: '/viewExam',
        name: 'viewExam',
        component: () => import('@/views/viewExam.vue'),
        meta: {
            'title': "查看考试详情"
        }
    },
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

router.beforeEach(async (to, from, next) => {
    let loginUser = await getLoginUser();
    if (loginUser === null) {
        console.log("用户获取失败");
        // alert(1);
        window.location.href = "/login";
    }
    //beforeEach是router的钩子函数，在进入路由前执行
    if (to.meta.title) {
        //判断是否有标题
        document.title = to.meta.title
    } else {
        document.title = '默认title'
    }
    next()
});

export default router
